//
//  TGMultilineTextField.m
//  TongGuanLease
//
//  Created by 张奇 on 2022/6/19.
//

#import "TGMultilineTextField.h"

@interface TGMultilineTextField ()<QMUITextViewDelegate>

@end

@implementation TGMultilineTextField

- (void)awakeFromNib {
    [super awakeFromNib];
    self.backgroundColor = UIColorClear;
    [self setupUI];
}

- (instancetype)init {
    if (self = [super init] ) {
        self.backgroundColor = UIColorClear;
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    [self addSubview:self.textView];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(7);
        make.bottom.mas_equalTo(0);
        make.right.mas_equalTo(3);
//        make.width.mas_equalTo(self.width);
        make.left.mas_equalTo(0);
        make.height.mas_equalTo(47);
    }];
//    UITextField *tf = [[UITextField alloc] init];
//    UILabel *placeholderLabel = [tf valueForKeyPath:@"placeholderLabel"];
//    _textView.placeholderColor = placeholderLabel.textColor;
}

- (void)setTextColor:(UIColor *)textColor {
    _textColor = textColor;
    self.textView.textColor = textColor;
}

- (NSString *)text {
    return self.textView.text;
}

- (void)setText:(NSString *)text {
    self.textView.text = text;
}

- (void)setPlaceholder:(NSString *)placeholder {
    _placeholder = placeholder;
    _textView.placeholder = placeholder;
}

- (void)setMaximumTextLength:(NSInteger)maximumTextLength {
    _maximumTextLength = maximumTextLength;
    self.textView.maximumTextLength = maximumTextLength;
}

- (void)textView:(QMUITextView *)textView newHeightAfterTextChanged:(CGFloat)height {
    
    CGFloat resultHeight = MAX(47, height);
    if (resultHeight != 47) {
        resultHeight = height + 13;
    }
    self.height = resultHeight;
    [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(resultHeight == 47 ? 7:5);
        make.height.mas_equalTo(resultHeight);
    }];
    if ([self.delegate respondsToSelector:@selector(multilineTextField:newHeightAfterTextChanged:)]) {
        [self.delegate multilineTextField:self newHeightAfterTextChanged:MAX(54, resultHeight)];
    }
}

- (void)drawRect:(CGRect)rect {
    self.textView.placeholder = self.placeholder;
}

- (QMUITextView *)textView {
    if (!_textView) {
        _textView = [[QMUITextView alloc] init];
        _textView.placeholder = @"请输入";
//        _textView.placeholderColor = TextViewPlaceholderColor;
        _textView.font = [UIFont fontWithName:Ping_Regular size:16];
        _textView.textColor = DeepBlackColor;
//        _textView.backgroundColor = RGB(246, 246, 246);
        _textView.backgroundColor = UIColorClear;
        _textView.contentInset = UIEdgeInsetsMake(0, 5, 0, 0);
        _textView.textAlignment = NSTextAlignmentRight;
        _textView.showsVerticalScrollIndicator = NO;
        _textView.showsHorizontalScrollIndicator = NO;
        _textView.delegate = self;
        
    }
    return _textView;
}

@end
